package cn.dnf.utils.test;

import cn.dnf.utils.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ESHomework {
    @Test
    public void testAdd(){
        //1.创建ES连接
        TransportClient client = ESClientUtil.getClient();
        IndexRequestBuilder requestBuilder = client.prepareIndex("pethome", "pet", "1");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id",3L);
        hashMap.put("name","翠花");
        hashMap.put("sex",1);
        IndexResponse indexResponse = requestBuilder.setSource(hashMap).get();
        System.out.println(indexResponse);

        client.close();



    }

    @Test
    public void testGet(){
        TransportClient client = ESClientUtil.getClient();
        GetRequestBuilder builder = client.prepareGet("pethome", "pet", "3");
        System.out.println(builder.get().getSourceAsMap());

        client.close();


    }

    @Test
    public void testUPdate(){
        TransportClient client = ESClientUtil.getClient();
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "pet", "3");
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("id","3L");
        hashMap.put("name","大如花");
        UpdateResponse updateResponse = updateRequestBuilder.setDoc(hashMap).get();
        System.out.println(updateResponse);

        client.close();


    }

    @Test
    public void testDel(){
        TransportClient client = ESClientUtil.getClient();
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "pet", "3");
        System.out.println(deleteRequestBuilder.get());

        client.close();


    }

    /**综合
     *
     */
    @Test
    public void test(){
        TransportClient client = ESClientUtil.getClient();

        //获取查询对象
        SearchRequestBuilder pethome = client.prepareSearch("pethome");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "我在源码");

        boolQueryBuilder.must(matchQueryBuilder);

        boolQueryBuilder
                .filter(QueryBuilders.termQuery("sex",1))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));
        //开启DSL查询
        pethome.setQuery(boolQueryBuilder);

        //分页
        pethome.setSize(2);
        pethome.setFrom(6);

        //排序
        pethome.addSort("age", SortOrder.DESC);

        SearchHit[] hits = pethome.get().getHits().getHits();

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsMap());
        }

        client.close();


    }

    /**
     * @Description: 批量添加数据
     * @Author: Director
     * @Date: 2022/7/23 0:02
     * @return: void
     **/
    @Test
    public void testBatch() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }
}
